﻿// -----------------------------------------------------------------------
// <copyright file="Picture.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace WPForms.Common.Model
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// This form field represents a picture
    /// </summary>
    [System.Runtime.Serialization.DataContract(IsReference = true)]
    [Description("Using this field users can select, or take photos and associate it with the form.")]
    public class PictureAttachment : FormField
    {
        /// <summary>
        /// Gets or sets the attachment id.
        /// </summary>
        [System.Runtime.Serialization.DataMember]
        public int? AttachmentId { get; set; }

        /// <summary>
        /// Writes the field specific data into the specified builder.
        /// </summary>
        /// <param name="builder">The builder.</param>
        public override void Write(StringBuilder builder)
        {
            if (this.AttachmentId.HasValue)
            {
                builder.Append(this.AttachmentId.Value);
            }
        }

        /// <summary>
        /// Intializez the field from the given stored value.
        /// </summary>
        /// <param name="storedValue">The stored value.</param>
        public override void Read(string storedValue)
        {
            int attachmentId;
            if (int.TryParse(storedValue, out attachmentId))
            {
                this.AttachmentId = attachmentId;
            }
        }

        /// <summary>
        /// Validates the element.
        /// Note!! The autogenerated NotImplementedException has to be removed!!
        /// </summary>
        /// <returns>
        /// The result of the validation.
        /// </returns>
        public override IEnumerable<string> Validate()
        {
            if (this.IsRequired && !this.AttachmentId.HasValue)
            {
                yield return this.Label + " is required";
            }
        }
    }
}
